import java.lang.reflect.Field;

import org.emet.entity.biz.Ceremony;

public class JavaCodeCreator {

	public static void main(String[] args) throws Exception {
		String className = Ceremony.class.getName();
		genDao(className);
		genServ(className);
		genCritera(className);
	}

	private static void genCritera(String className) throws Exception {
		Class<?> clazz = getClazz(className);
		print("package org.emet.service.dto;");
		print("public class " + clazz.getSimpleName() + "Criteria {");
		Field[] declaredFields = clazz.getDeclaredFields();
		for (Field field : declaredFields) {
			print("private " + field.getType().getName() + " " + field.getName() + ";");
		}
		print("}");
	}

	private static void genServ(String className) throws Exception {
		Class<?> clazz = getClazz(className);
		String lowerName = clazz.getSimpleName().substring(0, 1).toLowerCase() + clazz.getSimpleName().substring(1);
		print("package org.emet.service;");
		print("import org.emet.dao.*;");
		print("import java.sql.Timestamp;");
		print("import java.util.Date;");
		print("import org.emet.service.dto." + clazz.getSimpleName() + "Criteria;");
		print("import org.emet.entity.biz.*;");
		print("import org.emet.jee.BusinessException;");
		print("import org.emet.jee.core.dao.Page;");
		print("import org.slf4j.Logger;");
		print("import org.slf4j.LoggerFactory;");
		print("import org.hibernate.*;");
		print("@Service");
		print("@Transactional");
		print("@RemotingDestination");
		print("public class " + clazz.getSimpleName() + "Service {");
		{
			print("@Autowired");
			print("private " + clazz.getSimpleName() + "Dao " + lowerName + "Dao;");
			print("private static Logger logger = LoggerFactory.getLogger(" + clazz.getSimpleName() + "Service.class);");
			//CRUD
			print("public void save(" + clazz.getSimpleName() + " entity) {");
			{
				print("try {");
				//				print("if(entity.getId() == null && entity.getTimeStamp() == null){ entity.setTimeStamp(new Timestamp(new Date().getTime())); }");
				print(lowerName + "Dao.save(entity);");
				print("} catch (Exception e) {");
				print("logger.error(e.getMessage(), e);");
				print("throw new BusinessException(\"发生异常!\");");
				print("}");
			}
			print("}");

			print("	@Transactional(readOnly = true)");
			print("public " + clazz.getSimpleName() + " get" + clazz.getSimpleName() + "(Long id) {");
			{
				print("try {");
				print("return " + lowerName + "Dao.get(id);");
				print("} catch (ObjectNotFoundException e) {");
				print("logger.error(e.getMessage(), e);");
				print("throw new BusinessException(\"发生异常!\");");
				print("}");
			}
			print("}");

			print("public void del(Long... ids) {");
			{
				print("for (Long id : ids) {");
				{
					print("if (notExist(id)) {");
					print("continue;");
					print("}");
					{
						print("try {");
						print(lowerName + "Dao.delete(id);");
						print("} catch (Exception e) {");
						print("logger.error(e.getMessage(), e);");
						print("throw new BusinessException(\"发生异常!\");");
						print("}");
					}
				}
				print("}");
			}
			print("}");

			print("private boolean notExist(Long id) {");
			print("return get" + clazz.getSimpleName() + "(id) == null;");
			print("}");

			print("	@Transactional(readOnly = true)");
			print("public Page<" + clazz.getSimpleName() + "> search(Page<" + clazz.getSimpleName() + "> page, "
					+ clazz.getSimpleName() + "Criteria criteria) {");
			{
				print("try {return ");
				print(lowerName + "Dao.search(page, criteria);");
				print("} catch (Exception e) {");
				print("logger.error(e.getMessage(), e);");
				print("throw new BusinessException(\"发生异常!\");");
				print("}");
			}
			print("}");
		}
		print("}");
	}

	private static void genDao(String className) throws Exception {
		Class<?> clazz = getClazz(className);

		print("package org.emet.dao;");
		print("import org.emet.entity.biz.*;");
		print("import org.emet.service.dto." + clazz.getSimpleName() + "Criteria;");
		print("import org.emet.jee.core.dao.arch.*;");
		print("import org.springframework.stereotype.*;");
		print("@Repository");
		print("public class " + clazz.getSimpleName() + "Dao extends ArchDataSourceBaseDao<" + clazz.getSimpleName()
				+ ", Long> {");
		print("public Page<" + clazz.getSimpleName() + "> search(Page<" + clazz.getSimpleName() + "> page, "
				+ clazz.getSimpleName() + "Criteria criteria) {");
		{
			print("Criteria c = getSession().createCriteria(" + clazz.getSimpleName() + ".class);");
			print("NonEmptyValueHashMap<String, Object> propertyNameValues = new NonEmptyValueHashMap<String, Object>();");
			Field[] declaredFields = clazz.getDeclaredFields();
			for (Field field : declaredFields) {
				String fieldName = field.getName();
				print("propertyNameValues.put(\"" + fieldName + "\",criteria.get"
						+ fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) + "());");
			}
			print("c.add(Restrictions.allEq(propertyNameValues));");
			print("page.setOrderBy(\"id\");");
			print("page.setOrder(\"desc\");");
			print("return findPage(page, c);");
		}
		print("}");
		print("}");
	}

	private static Class<?> getClazz(String className) throws ClassNotFoundException {
		Class<?> clazz = Class.forName(className);
		return clazz;
	}

	private static void print(String string) {
		System.out.println(string);
	}

}
